<template>
	<!-- 分类选项卡 -->
	<view class="category-tabs-wrap" style="width: 98%;margin-left: 1%;"  >
		<!-- 吸顶 -->
		<view class="u-sticky-wrap" :class="[elClass]"
			:style="{ height: fixed ? height + 'px' : 'auto', backgroundColor: isSticky ? '#fff' : '#f1f1f1' }">
			<view class="u-sticky"
				:style="{ position: fixed ? 'fixed' : 'static', top: stickyTop + 'px', left: left + 'px', width: width == 'auto' ? 'auto' : width + 'px', zIndex: 1109 }">
				<view class="tabs-wrap u-p-y-20"
					:style="isSticky ? 'border-bottom:1px solid #eee;background-color:#fff' : ''">
					<scroll-view scroll-x class="tabs-content" enable-flex scroll-with-animation
						:scroll-left="scrollLeft">
						<view class="u-flex u-row-cetner u-flex-1 u-col-center">
							<view class="tab-item u-flex-col u-col-center u-row-center" 
								v-for="(item, index) in tabsList" :key="index" @tap="routerTo(item,index)" >
								<view class="tab-title u-m-y-6" :class="{ 'title-active': tabCur == index }" >
									{{ item.name }}</view>
								<view class="nav-line" :class="{ 'line-active':tabCur == index }"></view>
							</view>
						</view>
					</scroll-view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	/**
	 * category-tabs
	 * @description 一个可以吸顶的分类列表
	 * @property {Boolean} enable = false - 是否吸顶,tabbar项中，组件不会自动销毁，需要自行开关
	 * @property {Array} tabsList  - 分类列表
	 * @property {Array | Number} styleType  - 卡片类型
	 */
	let systemInfo = uni.getSystemInfoSync();
	// #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-QQ
	let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
	// #endif
 
	export default {
		components: {
		 
		},
		data() {
			return {
				// 吸顶相关
				fixed: false,
				height: 'auto',
				elClass: this.$u.guid(),
				left: 0,
				width: 'auto',
				stickyTop: 0,
				isSticky: true, //是否吸顶

				// 瀑布流 350-330
				addTime: 0, //排序间隙时间
				leftHeight: 0,
				rightHeight: 0,
				leftList: [],
				rightList: [],
				tempList: [],

				tabCur: 0,
				scrollLeft: 0,
				isEmpty: true,
				categoryId: this.tabsList[0]?.id,
				goodsList: [],
				tabCurrent: 0,
				loadStatus: 'loadmore', //loadmore-加载前的状态，loading-加载中的状态，nomore-没有更多的状态
				currentPage: 0,
				lastPage: 1
			};
		},
		watch: {
			enable(val) {
				if (val == false) {
					this.fixed = false;
					this.disconnectObserver('contentObserver');
				} else {
					this.initObserver();
				}
			}
		},
		props: {
			tabsList: {
				type: Array,
				default: () => []
			},
			styleType: {
				type: [String, Number],
				default: 1
			},
			enable: {
				type: Boolean,
				default: false
			}
		},
		created() {

		},
		mounted() {
			this.initObserver();
		},
		beforeDestroy() {
			this.disconnectObserver('contentObserver');
		},
		methods: {
			// 瀑布流相关
			async splitData() {
				if (!this.tempList.length) return;
				let item = this.tempList[0];
				if (!item) return;

				// 分左右
				if (this.leftHeight < this.rightHeight) {
					this.leftHeight += item.activity_discounts_tags.length ? 350 : 330;
					this.leftList.push(item);
				} else if (this.leftHeight > this.rightHeight) {
					this.rightHeight += item.activity_discounts_tags.length ? 350 : 330;
					this.rightList.push(item);
				} else {
					this.leftHeight += item.activity_discounts_tags.length ? 350 : 330;
					this.leftList.push(item);
				}

				// 移除临时列表的第一项，如果临时数组还有数据，继续循环
				this.tempList.splice(0, 1);
				if (this.tempList.length) {
					setTimeout(() => {
						this.splitData();
					}, this.addTime);
				}
			},
			clear() {
				this.leftList = [];
				this.rightList = [];
				this.leftHeight = 0;
				this.rightHeight = 0;
				this.tempList = [];
			},

			// 吸顶相关
			initObserver() {
				if (!this.enable) return;
				// #ifdef APP-PLUS || H5
				this.stickyTop = systemInfo.statusBarHeight + 44;
				// #endif
				// #ifdef MP
				let height = systemInfo.platform == 'ios' ? 44 : 48;
				let top = systemInfo.statusBarHeight + height;
				this.stickyTop = systemInfo.statusBarHeight + height;
				// #endif
				this.disconnectObserver('contentObserver');

				this.$uGetRect('.' + this.elClass).then(res => {
					this.height = res.height;
					this.left = res.left;
					this.width = res.width;
					this.$nextTick(() => {
						this.observeContent();
					});
				});
			},
			observeContent() {
				this.disconnectObserver('contentObserver');
				const contentObserver = this.createIntersectionObserver({
					thresholds: [0.95, 0.98, 1]
				});
				contentObserver.relativeToViewport({
					top: -this.stickyTop
				});
				contentObserver.observe('.' + this.elClass, res => {
					if (!this.enable) return;
					this.setFixed(res.boundingClientRect.top);
				});
				this.contentObserver = contentObserver;
			},
			setFixed(top) {
				const fixed = top < this.stickyTop;
				if (fixed) {
					this.isSticky = true;
				} else if (this.fixed) {
					this.isSticky = false;
				}
				this.fixed = fixed;
			},
			disconnectObserver(observerName) {
				const observer = this[observerName];
				observer && observer.disconnect();
			},

			// 商品列表
			getGoodsList() {

				this.$Router.push({
					path: '/pages/goods/categorydetailmodel',
					query: {
						id: this.categoryId
					}
				});
			},

			// tabs
			tabChange(index) {
				this.tabCur = index;
				this.scrollLeft = (index - 1) * 60;
				this.categoryId = this.tabsList[index].id;
				this.styleType === 1 && this.clear();
				this.goodsList = [];
				this.currentPage = 1;
				this.lastPage = 1;
				this.loadStatus = 'loadmore';
				this.getGoodsList();
			},
			routerTo(item,index) {
				this.tabCur = index
			console.log(item,index);
				let idlist = item.url.split('/');
					//console.info(JSON.stringify(item))
				if(item.url.indexOf("product/list/")!=-1){
					console.log(item.productCategories);
					this.$Router.push({
						path: "/pages/goods/categorydetailmodel",
						query: { id: item.productCategories,name:item.name}
					});
				}
				if(item.url.indexOf("/theme/detail/")!=-1){
					this.$Router.push({
						path: "/pages/goods/theme",
						query: { id: idlist[idlist.length-1]}
					});
				}
				if(item.url.indexOf("EXCHANGE")!=-1){
					this.$Router.push({
						path: "/pages/app/score/list",
						query: { id: idlist[idlist.length-1]}
					});
				}
				if(item.url.indexOf("ddjb")!=-1){
					this.$Router.push({
						path: "/pages/pdd/index",
						 
					});
				}
				if(item.url.indexOf("jifen")!=-1){
					this.$Router.push({
					 path: "/pages/goods/pointlist",
					});
				}
				if(item.url.indexOf("huitao")!=-1){
					this.$Router.push({
						path: "/pages/pdd/index",
					});
				}
				if(item.url.indexOf("chongzhi")!=-1){
					this.$Router.push({
						path: "/pages/user/chongzhi",
					});
				}
				if(item.url.indexOf("huikan")!=-1){
					// #ifdef APP
					this.$Router.push({
						//path: "/pages/dsp/dspindex",
						// path: "/pages/dsp/index",
						path: "/pages/dsp/dspindexapp",
					});
					// #endif
					// #ifdef MP
					this.$Router.push({
						path: "/pages/dsp/wxdsp",
					});
					// #endif
				}
			},
		}
	};
</script>

<style lang="scss" scoped>
	@mixin vue-flex($direction: row) {
		/* #ifndef APP-NVUE */
		display: flex;
		flex-direction: $direction;
		/* #endif */
	}

	.category-tabs-wrap {

		// 吸顶
		.u-sticky-wrap {
			background-color: #fff;
		}

		// 瀑布流
		.u-waterfall {
			@include vue-flex;
			flex-direction: row;
			align-items: flex-start;
		}

		.u-column {
			@include vue-flex;
			flex: 1;
			flex-direction: column;
			height: auto;
		}
	}

	// 滑动分类
	.tabs-content {
		white-space: nowrap;

		.tab-item {
			min-height: 60rpx;
			display: inline-block;
			margin: 0 10rpx;
			text-align: center;
		 
			.nav-line {
				width: 120rpx;
				height: 4rpx;
				background: #fff;
				 
			}
			
			.line-active {
				background: #D44C1C;
			}

			.tab-title {
				font-size: 28rpx;
				font-weight: 600;
				color: #333333;
			}

			.title-active {
				color: #D44C1C;
			} 

			.tab-des {
				font-size: 26rpx;
				font-weight: 400;
				color: #999999;
				text-align: center;
			}

			.des-active {
				background: linear-gradient(90deg, #e9b461, #eecc89);
				border-radius: 15rpx;
				padding: 2rpx 10rpx;
				color: #ffffff;
			}
			
		}
	}
	
</style>
